<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>餐桌添加</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/font.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/lib/layui/layui.js"
            charset="utf-8"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="x-body">
    <form class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label">
                <span class="x-red">*</span>餐桌类型
            </label>
            <div class="layui-input-inline">
                <select name="tableType">
                    <option disabled style='display: none' selected value='0'>请选择餐桌类型</option>
                    <option value="大厅">大厅</option>
                    <option value="卡座">卡座</option>
                    <option value="雅间">雅间</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="tableCode" class="layui-form-label">
                <span class="x-red">*</span>餐桌序号
            </label>
            <div class="layui-input-inline">
                <input type="number" id="tableCode" name="tableCode" required="" lay-verify="required"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="tableName" class="layui-form-label">
                <span class="x-red">*</span>餐桌名
            </label>
            <div class="layui-input-inline">
                <input type="text" id="tableName" maxlength="10" name="tableName" required="" lay-verify="required"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="tableCapacity" class="layui-form-label">
                <span class="x-red">*</span>餐桌容量
            </label>
            <div class="layui-input-inline">
                <input type="number" id="tableCapacity" name="tableCapacity" required="" lay-verify="capacity"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">
            </label>
            <button class="layui-btn" lay-filter="add" lay-submit="">
                添加
            </button>
        </div>
    </form>
</div>
<script>
    layui.use(['form', 'layer'], function () {
        $ = layui.jquery;
        var form = layui.form
            , layer = layui.layer;


        //自定义验证规则
        form.verify({
            capacity:function(value){
                if(value <= 0){
                    return '容量必须为正数';
                }
            },
            selectTableType:function(value){
                if(value == 0){
                    return '请选择餐桌类型！';
                }
            }
        });

        // 检测下拉选择框值的变化
        form.on('select', function (selectData) {
            $.ajax({
                type: "post",
                url: "${pageContext.request.contextPath}/table?m=findTableCodeByType",
                dataType: "json",
                data: {tableType: selectData.value},
                success: function (jsonData) {
                    if (jsonData.code === 200) {
                        $('#tableCode').val(jsonData.data);
                        if (selectData.value ==="大厅"){
                            $('#tableName').val(selectData.value+$('#tableCode').val()+"号桌");
                            $('#tableCapacity').val(10);
                        }else if (selectData.value==="雅间"){
                            $('#tableName').val($('#tableCode').val()+"号雅间");
                            $('#tableCapacity').val(15);
                        }else if (selectData.value==="卡座"){
                            $('#tableName').val(selectData.value+$('#tableCode').val()+"号桌");
                            $('#tableCapacity').val(4);
                        }
                    }
                }
            });
        });

        //输入框的值改变时触发
        $("#tableCode").on("input",function(e){
            //获取input输入的值
                if ($("select").val() ==="大厅"){
                    $('#tableName').val("大厅"+$('#tableCode').val()+"号桌");
                }else if ($("select").val()==="雅间"){
                    $('#tableName').val($('#tableCode').val()+"号雅间");
                }else if ($("select").val()==="卡座"){
                    $('#tableName').val("卡座"+$('#tableCode').val()+"号桌");
                }
        });

        //监听提交
        form.on('submit(add)', function (data) {
            if (data.field.tableType == 0){
                layer.msg("请选择餐桌类型！");
                return ;
            }
            //发异步，把数据提交给java
            $.ajax({
                type: "post",
                url: "${pageContext.request.contextPath}/table?m=add",
                data: data.field,
                dataType: "json",
                success: function (data) {
                    layer.msg(data.msg, function () {
                        if (data.code === 200) {
                            window.parent.location.reload();
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                        }
                    });
                }
            });
            return false;
        });
    });


</script>

</body>

</html>